package tom.client.admin;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.core.client.GWT;

import tom.client.DataForm;
import tom.client.FormLogin;
import tom.client.Library;
import tom.client.LookupDialog;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.CellTable;

import tom.shared.AppUser;
import tom.shared.Program;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.ListBox;

public class FormPermission extends DataForm {

	private ListDataProvider<Program> availForm = new ListDataProvider<Program>();
	private MultiSelectionModel<Program> availSelection = new MultiSelectionModel<Program>();
	private ListDataProvider<Program> permForm = new ListDataProvider<Program>();
	private MultiSelectionModel<Program> permSelection = new MultiSelectionModel<Program>();

	private final Button btnAdd;
	private final Button btnRemove;
	private final ListBox userLevel = new ListBox();
	private final ListBox workCompany = new ListBox();
	private final SimplePager pager1;
	private final SimplePager pager2;

	AsyncCallback<Boolean> updateCallback = new AsyncCallback<Boolean>() {

		@Override
		public void onSuccess(Boolean result) {
			Library.getAdminService().selectAvailForm(txtCode.getText(),
					workCompany.getItemText(workCompany.getSelectedIndex()),
					availCallback);
			Library.getAdminService().selectPermission(txtCode.getText(),
					workCompany.getItemText(workCompany.getSelectedIndex()),
					permCallback);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	AsyncCallback<List<Program>> availCallback = new AsyncCallback<List<Program>>() {

		@Override
		public void onSuccess(List<Program> result) {
			availForm.setList(result);
			// pager1.setPage(1);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	AsyncCallback<List<Program>> permCallback = new AsyncCallback<List<Program>>() {

		@Override
		public void onSuccess(List<Program> result) {
			permForm.setList(result);
			// pager2.setPage(1);
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	public FormPermission() {
		txtCode.setMaxLength(8);
		lbCode.setText("ชื่อ login");
		txtCode.setReadOnly(true);

		pager1 = new SimplePager();
		add(pager1, 23, 131);
		pager1.setSize("297px", "30px");

		CellTable<Program> table1 = new CellTable<Program>();
		table1.setPageSize(10);
		add(table1, 23, 164);
		table1.setSize("350px", "20px");

		TextColumn<Program> module1 = new TextColumn<Program>() {
			@Override
			public String getValue(Program object) {
				return object.module;
			}
		};
		module1.setCellStyleNames("table");
		table1.addColumn(module1, "ระบบ");
		table1.setColumnWidth(module1, "160px");

		TextColumn<Program> form1 = new TextColumn<Program>() {
			@Override
			public String getValue(Program object) {
				return object.pgmName;
			}
		};
		form1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		form1.setCellStyleNames("table");
		table1.addColumn(form1, "งาน");
		table1.setColumnWidth(form1, "180px");

		Label label = new Label("โปรแกรมที่เหลือ");
		add(label, 23, 111);

		CellTable<Program> table2 = new CellTable<Program>();
		table2.setPageSize(10);
		add(table2, 500, 164);
		table2.setSize("350px", "20px");

		TextColumn<Program> module2 = new TextColumn<Program>() {
			public String getValue(Program object) {
				return object.module;
			}
		};
		module2.setCellStyleNames("table");
		table2.addColumn(module2, "ระบบ");
		table2.setColumnWidth(module2, "160px");

		TextColumn<Program> form2 = new TextColumn<Program>() {
			public String getValue(Program object) {
				return object.pgmName;
			}
		};
		form2.setCellStyleNames("table");
		table2.addColumn(form2, "งาน");
		table2.setColumnWidth(form2, "180px");

		TextColumn<Program> level = new TextColumn<Program>() {
			public String getValue(Program object) {
				return object.level.toString();
			}
		};
		form2.setCellStyleNames("table");
		table2.addColumn(level, "ระดับ");
		table2.setColumnWidth(level, "50px");

		Label label_1 = new Label("โปรแกรมที่ใช้ได้");
		add(label_1, 497, 111);
		label_1.setSize("132px", "21px");

		pager2 = new SimplePager();
		add(pager2, 498, 131);
		pager2.setSize("297px", "30px");

		btnAdd = new Button("Add");
		btnAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Set<Program> set = availSelection.getSelectedSet();
				for (Program form : set) {
					Library.getAdminService().insertPermission(
							txtCode.getText(),
							workCompany.getItemText(workCompany
									.getSelectedIndex()), form.pgmId,
							userLevel.getSelectedIndex(), updateCallback);
				}
			}
		});
		btnAdd.setText("==>>");
		add(btnAdd, 399, 195);
		btnAdd.setSize("80px", "25px");

		btnRemove = new Button("Remove");
		btnRemove.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Set<Program> set = permSelection.getSelectedSet();
				for (Program form : set) {
					Library.getAdminService().deletePermission(
							txtCode.getText(),
							workCompany.getItemText(workCompany
									.getSelectedIndex()), form.pgmId,
							updateCallback);
				}
			}
		});
		btnRemove.setText("<<==");
		add(btnRemove, 399, 241);
		btnRemove.setSize("80px", "25px");

		availForm.addDataDisplay(table1);
		pager1.setDisplay(table1);
		table1.setSelectionModel(availSelection);

		permForm.addDataDisplay(table2);
		pager2.setDisplay(table2);
		table2.setSelectionModel(permSelection);

		userLevel.setName("userLevel");
		for (String txtLevel : Library.USER_LEVEL)
			userLevel.addItem(txtLevel);
		add(userLevel, 379, 164);
		userLevel.setSize("110px", "22px");

		workCompany.setName("workCompany");
		add(workCompany, 370, 74);
		workCompany.setSize("110px", "22px");
		workCompany.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				Library.getAdminService()
						.selectAvailForm(
								txtCode.getText(),
								workCompany.getItemText(workCompany
										.getSelectedIndex()), availCallback);
				Library.getAdminService()
						.selectPermission(
								txtCode.getText(),
								workCompany.getItemText(workCompany
										.getSelectedIndex()), permCallback);
			}
		});

		Label lblLevel = new Label("Level :");
		add(lblLevel, 379, 144);

		Label lbWork = new Label("Company :");
		add(lbWork, 290, 74);
		lbWork.setSize("74px", "18px");

	}

	private static FormPermission panel;

	public static FormPermission getPanel() {
		if (panel == null)
			panel = GWT.create(FormPermission.class);
		return panel;
	}

	@Override
	protected void search() {
		LookupDialog.show("ผู้ใช้", "userName", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getAdminService().selectMyInfo(txtCode.getText(), userCallback);
	}

	@Override
	public void setTitle(String title) {
		super.setTitle(title);
		txtCode.setText(FormLogin.frmLogin.getUser());
		refresh();
	}

	@Override
	protected void update() {
	}

	@Override
	protected void insert() {
	}

	@Override
	protected void delete() {
	}

	@Override
	protected String getReportFile() {
		return "report003.jasper";
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}

	private AppUser userRec;

	private AsyncCallback<List<String>> callback = new AsyncCallback<List<String>>() {

		@Override
		public void onSuccess(List<String> result) {
			String company = null;
			workCompany.clear();
			if (result!=null && result.size()>0) {
				for (String s : result)
					workCompany.addItem(s);
				company = workCompany.getItemText(1);
			} 
			Library.getAdminService().selectAvailForm(txtCode.getText(),
					company, availCallback);
			Library.getAdminService().selectPermission(txtCode.getText(),
					company, permCallback);
		}

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub

		}
	};

	private AsyncCallback<AppUser> userCallback = new AsyncCallback<AppUser>() {

		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(AppUser result) {
			userRec = result;
			// userLevel.setSelectedIndex(1);
			if (result == null) {
				availForm.setList(null);
				permForm.setList(null);
				Window.alert("ไม่พบชื่อผู้ใช้");
			} else {
				btnSave.setEnabled(false);
				btnDelete.setEnabled(false);

				pager1.setPage(0);
				pager2.setPage(0);
				Library.getAdminService().selectWorking(txtCode.getText(),
						callback);
			}
		}
	};

	@Override
	protected void getFields() {
	}
}
